<?php
/* SVN FILE: $Id: item_message.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Message item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/item_message.php $
*/

class item_message extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "A Message";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Store a message. {status}";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['unused'] = "No Message Stored, use item once to store a message.";
			$this->itemSettings['used'] = "A Message Stored";
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['unused'] = ipsRegistry::getClass('output')->formInput('item[unused]', $this->itemSettings['unused']);
		$form['used'] = ipsRegistry::getClass('output')->formInput('item[used]', $this->itemSettings['used']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['tags_for_description']}</label>
					<div style='display: inline-block'>
						{status} = {$this->lang->words['desc_message_status']}
						<br />
						{$this->lang->words['status_settable_below']}
					</div>
				</li>
				<li>
					<label>{$this->lang->words['unused_message']}</label>
					{$form['unused']}
				</li>
				<li>
					<label>{$this->lang->words['used_message']}</label>
					{$form['used']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['unused'] = $input['unused'];
		$setting['used'] = $input['used'];
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$setting = unserialize($invItem['settings']);
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($setting['message'])
		{
			IPSMember::getFunction()->removeAvatar($this->memberData['member_id']);
			
			IPSMember::save( $this->memberData['member_id'], array( 'extendedProfile' => array( 'avatar_type' => 'url', 'avatar_location' => $setting['avatar'], 'avatar_size' => $setting['dimensions'] ) ) );
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Viewed the message");
			
			return <<<EOF
<div id='messageShown'class='block_wrap'>
	<h3 id='messageShownHeader' class='maintitle'>{$this->item['item_name']}</h3>
{$setting['message']}
</div>
EOF;
		}
		if($this->request['request_method'] == 'post')
		{
			$message = IPSText::getTextClass('bbcode')->preDisplayParse(IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('message')));
			if(IPSText::getTextClass('bbcode')->error)
			{
				$this->registry->getClass('output')->showError(IPSText::getTextClass('bbcode')->error);
			}
			$settings = array('message' => $message);
			$this->DB->update('market_inventory',array(
														'settings' => serialize($settings),
							),'i_id='.$invItem['i_id']);
			
			$this->writeLog($invItem, "Stored a message");
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['message_stored'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$editor = IPSText::getTextClass('editor')->showEditor( '', 'message' );
		
		return <<<EOF
<form id='messageFormForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='messageForm'class='block_wrap'>
		<h3 id='messageFormHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['message_to_store']}</label>
					<br />
					{$editor}
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['save_message']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
	
	/**
	 * Get the description of the item, normally called when there is no intentory item being used
	 *
	 * @access	public
	 * @return	string
	 */
	public function parseStoreDescription()
	{
		return str_replace('{status}', '', $this->item['item_description']);
	}
	
	/**
	 * Get the description of the item, normally called when there is one intentory items being used
	 *
	 * @access	public
	 * @param	array	$invItem	The inventory item information
	 * @return	string
	 */
	public function parseInventoryDescription(array $invItem)
	{
		$setting = unserialize($invItem['settings']);
		return str_replace('{status}',$setting['message'] ? $this->itemSettings['used'] : $this->itemSettings['unused'],$this->item['item_description']);
	}
}
